﻿/*
   @Description:首页js
   @Author:wk
   @Date:2014-08-10 
 */
var Portal = {};

Portal.init = function () {
    var userNo = $("#currentIdentifyEmployeeNo").val();
    var userName = $("#currentIdentifyName").val().replace(" ", "");
    var empID = $("#currentIdentifyId").val();
    var depatmentKeyId = $("#currentIdentifyDepartmentKeyId").val();
    //外部资源编号，70为天津提供的图片链接编号(如有变动从这里更改就行)
    setTimeout(function () {
        var reqItemNo = {
        };
        $.AjaxPortal("/Portal/get-photo-externalresources", reqItemNo, function (data) {
            if (data != "") {
                if (data.length == 5) {
                    for (var i = 0; i < data.length; i++) {
                        if (data[i].ItemNo == "70") {
                            //顶部外挂图片
                            $("#topPhoto").attr("src", data[i].ItemValue + "?AgencyEmpNo=" + userNo + "&empID=" + empID);
                            $("#imgTopPhoto").css("display", "none");
                        }
                        if (data[i].ItemNo == "80") {
                            //底部外挂图片
                            $("#rightPhoto").attr("src", data[i].ItemValue + "?AgencyEmpNo=" + userNo + "&empID=" + empID);
                            $("#imgRightPhoto").css("display", "none");
                        }
                        //CCAI成交记录查询
                        if (data[i].ItemNo == "170") {
                            $("#systemPlatform").append(" <li><a id=\"CcaiFind\" href=\"/Portal/FindCcai?newUrl=" + data[i].ItemValue + "&empNo=" + userNo + "\" target=\"_blank\">CCAI成交查询</a></li>");
                            // $("#CcaiFind").attr("href", "/Portal/FindCcai?newUrl=" + data[i].ItemValue + "&empNo=" + userNo);
                        }
                        // //文字外挂链接(公司业务指引和网络营销平台)
                        if (data[i].ItemNo == "150") {

                            $("#systemPlatform").append("<li><a id=\"WorkPoint\" href=\"" + data[i].ItemValue + "?AgencyEmpNo=" + userNo + "&empID=" + empID + "&depatmentKeyId=" + depatmentKeyId + "\" target=\"_blank\">公司业务指引</a></li>");
                            // $("#WorkPoint").attr("href", data[i].ItemValue + "?AgencyEmpNo=" + userNo + "&empID=" + empID + "&depatmentKeyId=" + depatmentKeyId);
                        }
                        if (data[i].ItemNo == "160") {
                            $("#systemPlatform").append("<li><a id=\"NetworkMarketing\" href=\"" + data[i].ItemValue + "\" target=\"_blank\">网络营销平台</a></li>");
                            // $("#NetworkMarketing").attr("href", data[i].ItemValue);
                        }
                    }
                }
            }
            else {
                //顶部外挂图片
                $("#topPhoto").attr("src", "http://10.7.1.61:8011/TopNav.aspx?AgencyEmpNo=" + userNo + "&empID=" + empID);
                $("#imgTopPhoto").css("display", "none");
                //底部外挂图片
                $("#rightPhoto").attr("src", "http://10.7.1.61:8011/RightNav.aspx?AgencyEmpNo=" + userNo + "&empID=" + empID);
                $("#imgRightPhoto").css("display", "none");

                //文字外挂链接
                $("#WorkPoint").attr("href", "http://10.7.1.61/NewTJSmartBM/Index.html?AgencyEmpNo=" + userNo + "&empID=" + empID + "&depatmentKeyId=" + depatmentKeyId);

                $("#NetworkMarketing").attr("href", "http://10.4.18.67");

                $("#CcaiFind").attr("href", "http://10.7.1.18/Contract/Report/frmContractReportListForAgency.aspx ?empName=" + userName + "&empNo=" + userNo);
            }
        });
    }, 3500);

    //获取该员工本月的业绩和单数
    setTimeout(function () {
        var reqData = {
            "employeNo": userNo
        };
        $.AjaxPortal("/Portal/get-salesachievement-byempno", reqData, function (data) {
            if (data.Flag) {
                if (data.salesAchievement != null) {
                    $("#perBillCount").show();
                    $("#billCount").text(Number(data.salesAchievement.BillCount));
                    $("#performance").text(data.salesAchievement.Performance);
                }
                else {
                    $("#perBillCount").hide();
                }
            }
            else {
                $("#performance").text(data.ErrorMsg);
            }
        });
    }, 3000);
}

//站内信的方法
Portal.MessageDetail = function () {
    $("#allMessage li").each(function () {
        $(this).find(".reciverMessage").click(function () {
            window.location.href = "/center/message-details?keyId=" + $(this).attr("bbb");
        });
    });

    //弹层显示发该站内信的员工详细信息
    $("#allMessage li").each(function () {
        $(this).find(".messageList .userPhoto").click(function () {
            var reqData = {
                "userKeyId": $(this).next().val()
            };
            var divpanel = $(this).parent().next("#userDetail");
            $.AjaxAgency("/Portal/get-user-detail", reqData, function (data) {
                if (data.Flag) {
                    if (data.Result.Mobile == null) {
                        data.Result.Mobile = "无"
                    }
                    if (data.Result.WxNo == null) {
                        data.Result.WxNo = "无"
                    }
                    if (data.Result.Email == null) {
                        data.Result.Email = "无"
                    }
                    var DepartmentName = "";
                    if (data.Result.UserEmployeeDeptRoles.length > 0) {
                        for (var k = 0; k < data.Result.UserEmployeeDeptRoles.length; k++) {
                            DepartmentName += data.Result.UserEmployeeDeptRoles[k].DepartmentName;
                        };
                    }
                    divpanel.find("#messageDetail").empty();
                    var rowHtml = " <li class=\"EmployeeName\">" + data.Result.EmployeeName + "</li>"
                        + " <li class=\"DepartmentName\" title=\"" + DepartmentName + "\">" + DepartmentName + "</li>"
                        + "<li>电　话：<span class=\"Mobile\">" + data.Result.Mobile + "</span></li>"
                        + "<li>短　号：<span class=\"Cornet\">" + data.Result.Cornet + "</span></li>"
                        + "<li>邮　箱：<span class=\"Email\">" + data.Result.Email + "</span></li>"
                        + "<li>微信号：<span class=\"WxNo\">" + data.Result.WxNo + "</span></li>";
                    divpanel.find("#messageDetail").append(rowHtml);
                }
                divpanel.show();

                //全局变量
                operatePanelclick = function (e) {
                    var tempoint = divpanel.offset();
                    if (e.pageX > (tempoint.left + divpanel.width()) || e.pageX < tempoint.left || e.pageY > (tempoint.top + divpanel.height()) || e.pageY < tempoint.top) {
                        divpanel.hide();
                        $(document).unbind("click", operatePanelclick);
                    }
                };
                $(document).unbind("click", operatePanelclick);
                $(document).bind("click", operatePanelclick);
            });
        });
    });
};

//绘制图表(柱状堆叠图)
Portal.getStackedColumnChartData = function (allname, alldata, maxPerformance) {

    $('#container').highcharts({
        chart: {
            type: 'column',
            backgroundColor: "#fbfbfb", //图表背景色
            marginBottom: 80,
            width: 499,
            style: {
                width: 499    //可以控制container div的样式
            }
        },
        title: {
            text: '',
            style: { "color": "#333333", "fontSize": "14px", "fontWeight": "bold", "fontFamily": "SimSun" }
        },
        xAxis: {
            categories: allname,
            labels: {
                formatter: function () {
                    return this.value.split('|')[0];
                },
                style: { "fontSize": "10", "color": "#666", "fontFamily": "Microsoft YaHei" }
            }
        },
        yAxis: {
            min: 0,
            title: {
                text: '',
                style: { "fontFamily": "Arial", "color": "#666", "fontSize": "10" }
            },
            stackLabels: {
                enabled: true,  //控制柱状图顶部的数字是否显示
                rotation: -45,  //控制柱状图顶部的数字的旋转角度
                style: {
                    fontSize: "10",
                    fontWeight: 'bold',
                    color: (Highcharts.theme && Highcharts.theme.textColor) || 'gray'
                },
                y: -15  //控制顶部文字的垂直偏移量
            },
            labels: {
                formatter: function () {
                    return this.value;
                },
                style: { "fontFamily": "Arial", "color": "#666", "fontSize": "10" }
            },
            tickInterval: Math.floor(Number(maxPerformance) / 7)
        },
        legend: {
            align: 'right',
            x: -70,
            verticalAlign: 'top',
            y: 10,
            floating: true,
            backgroundColor: (Highcharts.theme && Highcharts.theme.legendBackgroundColorSolid) || 'white',
            borderColor: '#CCC',
            borderWidth: 1,
            shadow: false,
            enabled: false
        },
        tooltip: {
            formatter: function () {
                return '<b>' + this.x + '</b><br/>' +
                    this.series.name + ': ' + this.y + '<br/>' +
                    '总计: ' + this.point.stackTotal;
            },
            style: { fontFamily: "宋体", fontSize: "12", color: "#666" }
        },
        plotOptions: {
            column: {
                stacking: 'normal',
                dataLabels: {
                    enabled: false, //控制柱子上显示的数字
                    color: (Highcharts.theme && Highcharts.theme.dataLabelsColor) || 'white'
                },
            }
        },
        series: alldata
    });
};

//绘制图表(带标识的曲线图)
Portal.getIdentificationSplineData = function (allAverageName, allAverageData, maxPerformance) {

    $('#AverageContainer').highcharts({
        chart: {
            type: 'spline',
            backgroundColor: "#fbfbfb", //图表背景色
            marginBottom: 80,
            width: 500,
            style: {
                width: 500     //可以控制container div的样式
            }
        },
        title: {
            text: '',
            style: { "color": "#333333", "fontSize": "14px", "fontWeight": "bold", "fontFamily": "SimSun" }
        },
        xAxis: {
            categories: allAverageName,
            labels: {
                enabled: false,
                style: { "fontSize": "10", "color": "#666", "fontFamily": "Microsoft YaHei" }
            }
        },
        yAxis: {
            min: 0,  //不显示负数刻度值
            title: {
                text: '',
                style: { "fontFamily": "Arial", "color": "#666", "fontSize": "10" }
            },
            labels: {
                formatter: function () {
                    return this.value
                },
                style: { "fontFamily": "Arial", "color": "#666", "fontSize": "10" }
            },
            tickInterval: Math.floor(Number(maxPerformance) / 7)
        },
        legend: {
            enabled: false
        },
        tooltip: {
            crosshairs: true,
            formatter: function () {
                var msg = '<b>' + this.x + '</b><br/>' + this.series.name + ': ' + this.y + '<br/>';
                var str = "";
                if (this.point.x != 0) {
                    var prevValue = this.series.data[this.point.x - 1].y;
                    var lastValue = prevValue - this.y;
                    var str = '您距离上一名业绩还有:' + lastValue.toFixed(2) + '差距';
                }
                return msg + str;
            },
            style: { fontFamily: "宋体", fontSize: "12", color: "#666" }
        },
        plotOptions: {
            spline: {
                marker: {
                    radius: 0, //将marker点的半径改为0就不显示了
                    lineColor: '#666666',
                    lineWidth: 1
                }
            }
        },
        series: [{
            name: '业绩',
            marker: {
                symbol: 'square'
            },
            data: allAverageData
        }]
    });
};

//降价房源图片的切换显示
Portal.ReducePricePropertyPictureSwitch = function () {
    //总数量
    var num = $(".reducePrice > li").length;
    //初始值
    var i = 0;
    var j = 0;
    //上一张
    $(".prev").click(function () {
        if (i > 1) {
            j = i - 2;
            if (i > 1) {
                $(".reducePrice > li").eq(i).fadeOut();
                $(".reducePrice > li").eq(i + 1).fadeOut();

                $(".reducePrice > li").eq(j).fadeIn();
                $(".reducePrice > li").eq(j + 1).fadeIn();
                i = j;
            }
            else {
                $(".reducePrice > li").eq(i).fadeOut();
                $(".reducePrice > li").eq(j + 1).fadeIn();
                i = 0;
            }
        } else {
            $(".reducePrice > li").fadeOut();
            i = $(".reducePrice > li").length;
            if (i % 2 == 0) {
                $(".reducePrice > li").eq(i - 1).fadeIn();
                $(".reducePrice > li").eq(i - 2).fadeIn();
                i = i - 2;
            }
            else {
                $(".reducePrice > li").eq(i - 1).fadeIn();
                i = i - 1;
            }
        }
    });
    //下一张
    $(".next").click(function () {
        if (i < num - 2) {
            $(".reducePrice > li").eq(i).fadeOut();
            $(".reducePrice > li").eq(i + 1).fadeOut();
            j = i + 2;
            if (num - 2 - i > 1) {
                $(".reducePrice > li").eq(j).fadeIn();
                $(".reducePrice > li").eq(j + 1).fadeIn();
                i = j;
            }
            else {
                $(".reducePrice > li").eq(j).fadeIn();
                if (num % 2 > 0) {
                    i = num - 1;
                }
                else {
                    i = num - 2;
                }
            }
        } else {
            i = i - i;
            $(".reducePrice > li").eq(i).fadeIn();
            $(".reducePrice > li").eq(i + 1).fadeIn();
            num = $(".reducePrice > li").length;
        }
    });
};

//系统消息切换（根据类型切换显示）
Portal.InformationSwitch = function () {
    $(".allOption > li").each(function () {
        $(this).click(function () {
            var includedInformationType;
            $(".showinfos").empty();
            if ($(this).attr("id") == "turnproinfo") {
                includedInformationType = 1;
            }
            else if ($(this).attr("id") == "turninqinfo") {
                includedInformationType = 101;
            }
            else if ($(this).attr("id") == "completeinfo") {
                includedInformationType = 201;
            }
            else {
                includedInformationType = 0;
            }
            var reqData = {
                "includedInformationType": includedInformationType
            };
            $.AjaxAgency("/Portal/get-new-informationpartial", reqData, function (data) {
                if (data.Flag) {
                    if (data.Result != null && data.Result.length > 0) {
                        var htmlContext = "";
                        for (var i = 0; i < data.Result.length; i++) {
                            if (data.Result[i].InformationCategory == 1 || data.Result[i].InformationCategory == 3) {
                                htmlContext += "<li><a href='/property/details?keyId=" + data.Result[i].SourceObjectKeyId + "' title='" + data.Result[i].Content + "' target='_blank'>" + data.Result[i].Content + "</a></li>";
                            } else if (data.Result[i].InformationCategory == 2) {
                                if (data.Result[i].InformationTypeE == 101) {
                                    if (data.Result[i].Remark == "send")
                                    { 
                                        htmlContext += "<li><a href='/customer/inquiry-details?keyId=" + data.Result[i].SourceObjectKeyId + "' title='" + data.Result[i].Content + "' target='_blank'>" + data.Result[i].Content + "</a></li>";
                                    }
                                    else
                                    {
                                        htmlContext += "<li title='" + data.Result[i].Content + "'>" + data.Result[i].Content + "</li>";
                                    }
                                }
                            }
                            else {
                                htmlContext += "<li title='" + data.Result[i].Content + "'>" + data.Result[i].Content + "</li>";
                            }
                        }
                    }
                    else {
                        htmlContext = "<li class=\"noDataImg\">还没有人给您发系统消息</li>";
                    }
                }
                else {
                    htmlContext = "<li>" + data.ErrorMsg + "</li>";
                }
                $(".showinfos").append(htmlContext);
            });
            //改变样式
            $(this).addClass("current").siblings().removeClass("current");
        });
    });
};

//绘制图表（显示值的柱状图）
Portal.getColumnChartData = function (allname, alldata, maxPerformance) {
    $('#container').highcharts({
        chart: {
            type: 'column',
            backgroundColor: "#fbfbfb", //图表背景色
            marginBottom: 80,
            width: 499,
            style: {
                width: 499    //可以控制container div的样式
            }
        },
        title: {
            text: '',
            style: { "color": "#333333", "fontSize": "14px", "fontWeight": "bold", "fontFamily": "SimSun" }
        },
        xAxis: {
            categories: allname,
            labels: {
                style: { "fontSize": "10", "color": "#666", "fontFamily": "Microsoft YaHei" },
                formatter: function () {
                    return this.value.split('|')[0];
                }
            }
        },
        yAxis: {
            min: 0,
            title: {
                text: '',
                style: { "fontFamily": "Arial", "color": "#666", "fontSize": "10" }
            },
            stackLabels: {
                enabled: true,  //控制柱状图顶部的数字是否显示
                rotation: -45,  //控制柱状图顶部的数字的旋转角度
                style: {
                    fontSize: "10",
                    fontWeight: 'bold',
                    color: (Highcharts.theme && Highcharts.theme.textColor) || 'gray'
                },
                y: -15  //控制顶部文字的垂直偏移量
            },
            labels: {
                formatter: function () {
                    return this.value
                },
                style: { "fontFamily": "Arial", "color": "#666", "fontSize": "10" }
            },
            tickInterval: Math.floor(Number(maxPerformance) / 7)
        },
        legend: {
            enabled: false
        },
        tooltip: {
            formatter: function () {
                return '<b>' + this.x + '</b><br/>' +
                    '总计: ' + '<b>' + this.y + '元</b>';
            },
            style: { fontFamily: "宋体", fontSize: "12", color: "#666" }
        },
        series: [{
            data: alldata,
            dataLabels: {
                enabled: true,
                rotation: -45,
                x: 4,
                y: -15,
                style: {
                    fontSize: "10",
                    fontWeight: 'bold',
                    color: "#666"
                }
            }
        }]
    });
};

//首页异步加载方法
$.extend({
    AjaxPortal: function (url, reqData, fun) {
        $.ajax({
            url: url,
            data: reqData,
            type: "POST",
            success: function (data) {
                fun(data);
            }
        });
    }
});
